#include "stdafx.h"

#include "..\ConsoleColor.h"

namespace Fast
{

// N.B.:  these tests are pretty wimpy.  Actually need eyeballs for this one.

TESTCLASS(ConsoleColorTests)
{
	HANDLE m_handle;

	virtual void ClassInitialize()
	{
		m_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);

		WORD attributes;
		if (false == GetAttributes(attributes))
			m_handle = INVALID_HANDLE_VALUE;
	}
	// nothing to close

	bool GetAttributes(WORD& attributes) const
	{
		CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
		BOOL b = ::GetConsoleScreenBufferInfo(m_handle, &csbi);
		attributes = csbi.wAttributes;
		return b == TRUE;
	}
	WORD GetAttributes() const
	{
		WORD attributes;
		GetAttributes(attributes);
		return attributes;
	}

	TESTMETHOD(SetFontColorResetTest)
	{
		if (m_handle == INVALID_HANDLE_VALUE)
			return; // can't run test in GUI mode

		WORD before = GetAttributes();
		{
			ConsoleColor cc;
			cc.SetFontColor(0xF&(~before)); // change it to something other than what it is now
			TDD_VERIFY(before != GetAttributes());
		}
		// here, we take advantage of deconstruction of Class ConsoleColor, which invoke method Reset()
		TDD_VERIFY(before == GetAttributes());
	}
	TESTMETHOD(SetFontColor)
	{
		if (m_handle == INVALID_HANDLE_VALUE)
			return; // can't run test in GUI mode

		ConsoleColor cc;
		cc.SetFontColor(ConsoleColor::Yellow);
		WORD attr = GetAttributes();
		TDD_VERIFY(14 == (attr&0xF)); // yellow == 14
	}

	TESTMETHOD(SetFontAndBackgroundColorsResetTest)
	{
		if (m_handle == INVALID_HANDLE_VALUE)
			return; // can't run test in GUI mode

		WORD before = GetAttributes();
		{
			WORD bg = before>>4;
			WORD fc = before&0xF;

			ConsoleColor cc;
			cc.SetFontAndBackgroundColors(0xF&(~bg), 0xF&(~fc)); // change both fore- and back-ground colors to something other than what it is now
			TDD_VERIFY(before != GetAttributes());
		}
		// here, we take advantage of deconstruction of Class ConsoleColor, which invoke method Reset()
		TDD_VERIFY(before == GetAttributes());
	}

	TESTMETHOD(SetFontAndBackgroundColor)
	{
		if (m_handle == INVALID_HANDLE_VALUE)
			return; // can't run test in GUI mode

		ConsoleColor cc;
		cc.SetFontAndBackgroundColors(ConsoleColor::Red|ConsoleColor::Blink, ConsoleColor::Blue);

		WORD attr = GetAttributes();
		WORD fc = attr&0xF;
		WORD bc = (attr&0x7F)>>4; // AND off blink bit
		WORD bb =  attr&0x80;
		TDD_VERIFY(4 == fc); // red == 4
		TDD_VERIFY(1 == bc); // blue == 1
		TDD_VERIFY(bb != 0); // blink
	}
};

}
